import generator
import alg
import time
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
import matplotlib.pyplot as plt


time_x=[2000,4000,6000,8000,10000]
sort_name=["插入排序","堆排序","冒泡排序","二分归并排序","快速排序"]
insertionSort_time=[]
heap_sort_time=[]
bubbleSort_time=[]
sort_merge_time=[]
quick_sort_time=[]
sort_time=[insertionSort_time,
heap_sort_time,
bubbleSort_time,
sort_merge_time,
quick_sort_time,]


def test():
    for i in range(5):
        list_1=generator.random_list(time_x[i])
        list_2=generator.random_list(time_x[i])
        list_3=generator.random_list(time_x[i])
        list_4=generator.random_list(time_x[i])
        list_5=generator.random_list(time_x[i])

        start_time=time.time()
        for j in range(10):
            alg.insertionSort(list_1[j])
        end_time=time.time()
        insertionSort_time.append((end_time-start_time)/10)


        start_time_2=time.time()
        for j in range(10):
            alg.bubbleSort(list_2[j])
        end_time_2=time.time()
        bubbleSort_time.append((end_time_2-start_time_2)/10)

        
        start_time_3=time.time()
        for j in range(10):
            alg.heap_sort(list_3[j])
        end_time_3=time.time()
        heap_sort_time.append((end_time_3-start_time_3)/10)

        start_time_4=time.time()
        for j in range(10):
            alg.sort_merge(list_4[j])
        end_time_4=time.time()
        sort_merge_time.append((end_time_4-start_time_4)/10)

        start_time_5=time.time()
        for j in range(10):
            alg.quick_sort(list_5[j],0,len(list_5[i])-1)
        end_time_5=time.time()
        quick_sort_time.append((end_time_5-start_time_5)/10)


    for i in range(5):
        plt.rcParams["font.sans-serif"]=['SimHei']
        plt.rcParams["axes.unicode_minus"]=False
        plt.bar(time_x,sort_time[i],1000)
        for a,b,k in zip(time_x,sort_time[i],range(len(time_x))): # zip 函数
            plt.text(a,b,"%.2f"%sort_time[i][k],ha='center',fontsize=20) 
        plt.title(sort_name[i]+"时间分析")
        plt.xlabel("数据量")
        plt.ylabel("时间")
        plt.show()


test()